/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-03 10:17:20
 * @LastEditTime: 2023-01-16 15:41:50
 */
import {
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_FIRST_AT,
  SET_TARGET_AT,
  SET_SOURCE_AT,
  SET_SELECT_KEY,
  GET_TATLIST,
  ADD_TATLIST,
  GET_CURRENT,
  DEL_TATLIST,
  EDIT_TATLIST
} from '../constants';
import { delItemByIdArray, updateItemById, insertItem } from '@/utils/list';

export const MappingRelationReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };
    /**
     * @description: 设置选中的档案类型
     */
    case SET_FIRST_AT:
      return {
        ...state,
        selectAT: action.data?.length > 0 ? action.data[0] : {}
      };
    /* 目标选择项 */
    case SET_TARGET_AT:
      return {
        ...state,
        targetItems: action.data
      };
    case SET_SOURCE_AT:
      return {
        ...state,
        fileList: action.data
      };
    case SET_SELECT_KEY: {
      return { ...state, selectItems: action.data };
    }
    case GET_CURRENT: {
      return { ...state, current: action.data };
    }
    case GET_TATLIST: {
      return { ...state, TATList: action.data };
    }
    /* 新增 */
    case ADD_TATLIST: {
      return { ...state, TATList: insertItem(action.data, state.TATList) };
    }
    /* 修改 */
    case EDIT_TATLIST: {
      return { ...state, TATList: updateItemById(action.data.id, action.data, state.TATList) };
    }
    /* 删除 */
    case DEL_TATLIST: {
      return { ...state, TATList: delItemByIdArray(action.ids, state.TATList) };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
